package net.zabuchy.meeting.action;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import net.zabuchy.address.entity.Address;
import net.zabuchy.helper.AddressHelper;
import net.zabuchy.helper.ParamNames;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.issue.entity.Issue;
import net.zabuchy.meeting.bo.MeetingBo;
import net.zabuchy.meeting.entity.Meeting;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import freemarker.template.utility.StringUtil;

public class CreateMeetingAction extends ActionSupport
	implements ServletRequestAware,ServletResponseAware{ 
	
	private static final Logger LOGGER = Logger.getLogger(CreateMeetingAction.class.getName());
	
	@Autowired
	MeetingBo meetingBo;
	@Autowired
	UserBo userBo;
	@Autowired
    private SessionHelper sessionBean;
	
	private HttpServletRequest request;
    private HttpServletResponse response;
    
    Meeting meeting;
	
	private Integer meetingId;
	private Integer customerId;
	private String author;
	private String title;
	private String description;
	private String leading;
	private String leadingChoose;
	private Date creationDate;
	private Date meetingDate;
	private String meetingTime;
	private String leadingNickname;
	private String place;
	
	private String tmpParticipants;
	
	private Set<User> participants = new HashSet<User>(0);
	private String participant;
	
	/**********************************************/
	private String city;
	private String country;
	private String address1;
	private String state;
	private String zipcode;
	private String[] countries = AddressHelper.getCountries();
	/**********************************************/
	
	private List<String> users = new ArrayList<String>(0);
	private Integer userId;
	
	
	public String execute() {
		

		LOGGER.debug("addForm");
		String returnInputMeetingForm = request.getParameter(ParamNames.CREATE_NEW_MEETING_FORM);
		
		boolean returnForm = (returnInputMeetingForm != null ? 
				Boolean.parseBoolean(returnInputMeetingForm) : false);
		
		List<User> tmp = userBo.findByCustomerId(sessionBean.getCustomerId());
		Iterator it = tmp.iterator();
		while(it.hasNext()) {
			User user = (User) it.next();
			users.add(user.getUsername());
		}
		
		if(returnForm) {
			LOGGER.debug("clear all fields and return form");
			clearFields();
			return Action.INPUT;
		}
		
		if(leadingChoose.equals("1")) {
			leading =  sessionBean.getExecutor().getUsername();
		} else {
			leading = leadingNickname;
		}
		LOGGER.debug("meetingdate: " + meetingDate);
		meeting = new Meeting();
		meeting.setTitle(title);
		meeting.setDescription(description);
		meeting.setMeetingDate(meetingDate);
		meeting.setMeetingTime(meetingTime);
		meeting.setPlace(place);
		meeting.setLeading(leading);
		
		meetingBo.add(meeting);
		meetingId = meeting.getMeetingId();
		
		return Action.SUCCESS;
	}
	
	public String addMeetingAddress() {
		LOGGER.debug("meetingId: " + meetingId);
		Address address = new Address();
		address.setCity(city);
		address.setCountry(country);
		address.setState(state);
		address.setZipcode(zipcode);
		address.setAddress1(address1);
		
		meeting = meetingBo.findById(meetingId);
		meeting.setAddress(address);
		meetingBo.update(meeting);
		
		return Action.SUCCESS;
	}
	
	public String setParticipants() {
		
		User user = userBo.findByUsername(participant);
		participants.add(user);
		meeting.setParticipants(participants);
		meetingBo.update(meeting);
		
		return Action.SUCCESS;
	}
	
	public String removeParticipants() {
		
		if (userBo == null) {
            return Action.ERROR;
        }
        User user = userBo.findById(userId);
        if (user == null) {
            return Action.ERROR;
        }
        
        LOGGER.debug("Remove user " + user.getUsername() + " with id " + user.getUserId() + " from meeting: " + meetingId);
        LOGGER.debug("participants.size: " + participants.size());
        Iterator it = participants.iterator();
        Set<User> temporary = new HashSet<User>(0);
        while(it.hasNext()) {
        	User tmp = (User) it.next();
        	if(user.getUserId() != tmp.getUserId()) {
        		temporary.add(tmp);
        	}
        }
        participants.clear();
        participants.addAll(temporary);
        LOGGER.debug("participants.size: " + participants.size());
        meeting.setParticipants(participants);
        meetingBo.update(meeting);
        
		return Action.SUCCESS;
	}
	
	private void clearFields() {
		LOGGER.debug("TO DO");
	}

	public void setServletRequest(HttpServletRequest request){
		  this.request = request;
		  }

		  public HttpServletRequest getServletRequest(){
		  return request;
		  }

		  public void setServletResponse(HttpServletResponse response){
		  this.response = response;
		  }

		  public HttpServletResponse getServletResponse(){
		  return response;
		  }

		public MeetingBo getMeetingBo() {
			return meetingBo;
		}

		public void setMeetingBo(MeetingBo meetingBo) {
			this.meetingBo = meetingBo;
		}

		public UserBo getUserBo() {
			return userBo;
		}

		public void setUserBo(UserBo userBo) {
			this.userBo = userBo;
		}

		public SessionHelper getSessionBean() {
			return sessionBean;
		}

		public void setSessionBean(SessionHelper sessionBean) {
			this.sessionBean = sessionBean;
		}

		public Integer getMeetingId() {
			return meetingId;
		}

		public void setMeetingId(Integer meetingId) {
			this.meetingId = meetingId;
		}

		public Integer getCustomerId() {
			return customerId;
		}

		public void setCustomerId(Integer customerId) {
			this.customerId = customerId;
		}

		public String getAuthor() {
			return author;
		}

		public void setAuthor(String author) {
			this.author = author;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public String getLeading() {
			return leading;
		}

		public void setLeading(String leading) {
			this.leading = leading;
		}

		public Date getCreationDate() {
			return creationDate;
		}

		public void setCreationDate(Date creationDate) {
			this.creationDate = creationDate;
		}

		public Date getMeetingDate() {
			return meetingDate;
		}

		public void setMeetingDate(Date meetingDate) {
			this.meetingDate = meetingDate;
		}

		public String getPlace() {
			return place;
		}

		public void setPlace(String place) {
			this.place = place;
		}

		public String getTmpParticipants() {
			return tmpParticipants;
		}

		public void setTmpParticipants(String tmpParticipants) {
			this.tmpParticipants = tmpParticipants;
		}

		public Set<User> getParticipants() {
			return participants;
		}

		public void setParticipants(Set<User> participants) {
			this.participants = participants;
		}

		public HttpServletRequest getRequest() {
			return request;
		}

		public void setRequest(HttpServletRequest request) {
			this.request = request;
		}

		public HttpServletResponse getResponse() {
			return response;
		}

		public void setResponse(HttpServletResponse response) {
			this.response = response;
		}

		public String getMeetingTime() {
			return meetingTime;
		}

		public void setMeetingTime(String meetingTime) {
			this.meetingTime = meetingTime;
		}

		public String getLeadingNickname() {
			return leadingNickname;
		}

		public void setLeadingNickname(String leadingNickname) {
			this.leadingNickname = leadingNickname;
		}

		public Meeting getMeeting() {
			return meeting;
		}

		public void setMeeting(Meeting meeting) {
			this.meeting = meeting;
		}

		public String getLeadingChoose() {
			return leadingChoose;
		}

		public void setLeadingChoose(String leadingChoose) {
			this.leadingChoose = leadingChoose;
		}

		public String getCity() {
			return city;
		}

		public void setCity(String city) {
			this.city = city;
		}

		public String getCountry() {
			return country;
		}

		public void setCountry(String country) {
			this.country = country;
		}

		public String getAddress1() {
			return address1;
		}

		public void setAddress1(String address1) {
			this.address1 = address1;
		}

		public String getState() {
			return state;
		}

		public void setState(String state) {
			this.state = state;
		}

		public String getZipcode() {
			return zipcode;
		}

		public void setZipcode(String zipcode) {
			this.zipcode = zipcode;
		}

		public String[] getCountries() {
			return countries;
		}

		public void setCountries(String[] countries) {
			this.countries = countries;
		}

		/**
		 * @param users the users to set
		 */
		public void setUsers(List<String> users) {
			this.users = users;
		}

		/**
		 * @return the users
		 */
		public List<String> getUsers() {
			return users;
		}

		/**
		 * @param participant the participant to set
		 */
		public void setParticipant(String participant) {
			this.participant = participant;
		}

		/**
		 * @return the participant
		 */
		public String getParticipant() {
			return participant;
		}

		public Integer getUserId() {
			return userId;
		}

		public void setUserId(Integer userId) {
			this.userId = userId;
		}
	
	
	}